package com.ruoyi.project.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.project.service.email.EmailService;
import com.ruoyi.project.service.email.VerificationCodeService;
import com.ruoyi.system.api.RemoteAiExperimentService;
import com.ruoyi.system.api.RemoteEmailService;
import com.ruoyi.system.api.RemoteUserService;
import com.ruoyi.system.api.domain.AiExperiment;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.project.domain.ProjectResearch;
import com.ruoyi.project.service.IProjectResearchService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 科研项目Controller
 * 
 * @author czj
 * @date 2025-01-06
 */
@RestController
@RequestMapping("/research")
public class ProjectResearchController extends BaseController
{
    @Autowired
    private IProjectResearchService projectResearchService;
    @Autowired
    private RemoteAiExperimentService remoteAiExperimentService;
    @Autowired
    private RemoteUserService remoteUserService;
    @Autowired
    private RemoteEmailService remoteEmailService;


    /**
     * 查询科研项目列表
     */
    @RequiresPermissions("project:research:list")
    @GetMapping("/list")
    public TableDataInfo list(ProjectResearch projectResearch)
    {
        startPage();
        List<ProjectResearch> list = projectResearchService.selectProjectResearchList(projectResearch);
        for(ProjectResearch projectResearch1:list){
            projectResearch1.setCorrentUid(SecurityUtils.getUserId());
        }
        return getDataTable(list);
    }

    /**
     * 查找对应数据历史
     *
     * @param projectId
     * @return
     */
    @GetMapping("/tracklist/{projectId}")
    public TableDataInfo Tracklist(@PathVariable Long projectId){
        startPage();
        List<ProjectResearch> tracklist=projectResearchService.selectProjectResearchTrackList(projectId);
        return getDataTable(tracklist);
    }
    /**
     * 导出科研项目列表
     */
    @RequiresPermissions("project:research:export")
    @Log(title = "科研项目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProjectResearch projectResearch)
    {
        List<ProjectResearch> list = projectResearchService.selectProjectResearchList(projectResearch);
        ExcelUtil<ProjectResearch> util = new ExcelUtil<ProjectResearch>(ProjectResearch.class);
        util.exportExcel(response, list, "科研项目数据");
    }

    /**
     * 获取科研项目详细信息
     */
    @RequiresPermissions("project:research:query")
    @GetMapping(value = "/{projectId}")
    public AjaxResult getInfo(@PathVariable("projectId") Long projectId)
    {
        ProjectResearch projectResearch=projectResearchService.selectProjectResearchByProjectId(projectId);
        R<LoginUser> loginUserR = remoteUserService.getUserInfo(SecurityUtils.getUsername(), SecurityConstants.FROM_SOURCE);
        LoginUser loginUser = loginUserR.getData();
        projectResearch.setClockedemail(loginUser.getSysUser().getEmail());
        return success(projectResearch);
    }

    /**
     * 新增科研项目
     */
    @RequiresPermissions("project:research:add")
    @Log(title = "科研项目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProjectResearch projectResearch)
    {
        return toAjax(projectResearchService.insertProjectResearch(projectResearch));
    }

    /**
     * 修改科研项目
     */
    @RequiresPermissions("project:research:edit")
    @Log(title = "科研项目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProjectResearch projectResearch)
    {
        if(projectResearch.getInitiatorId()!=SecurityUtils.getUserId()) {
            projectResearch.setApproverId(SecurityUtils.getUserId());
        }else{
            projectResearch.setApproverId(null);
        }
        if(projectResearch.getApprovalStatus()==2){
            if(projectResearch.getCategoryType().equals("3")){
                AiExperiment aiExperiment=new AiExperiment();
                aiExperiment.setProjectId(projectResearch.getProjectId());
                remoteAiExperimentService.add(aiExperiment);
            }
        }
        return toAjax(projectResearchService.updateProjectResearch(projectResearch));
    }

    /**
     * 删除科研项目
     */
    @RequiresPermissions("project:research:remove")
    @Log(title = "科研项目", businessType = BusinessType.DELETE)
	@DeleteMapping("/{projectIds}")
    public AjaxResult remove(@PathVariable Long[] projectIds)
    {
        return toAjax(projectResearchService.deleteProjectResearchByProjectIds(projectIds));
    }


    @GetMapping("/project/{projectId}")
    public R<ProjectResearch> getProjectInfo(@PathVariable("projectId") Long projectId){
        ProjectResearch projectResearch=projectResearchService.selectProjectResearchByProjectId(projectId);
        return R.ok(projectResearch);
    }

    @PostMapping("/trackback")
    public AjaxResult doTrackBack(@RequestBody ProjectResearch projectResearch){
        return toAjax(projectResearchService.trackBackProjectResearch(projectResearch));
    }

    // 生成验证码并发送到邮箱
    @GetMapping("/sendCode")
    public AjaxResult sendVerificationCode() {
        remoteEmailService.sendEmail();
        return success();
    }

    @PostMapping("/verifyCode/{VerificationCode}")
    public AjaxResult verifyVerificationCode(@PathVariable("VerificationCode") String verificationCode){
        System.out.println(verificationCode);
        return success(remoteEmailService.verifyCode(verificationCode));
    }

}
